package liuds.music.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Vibrator;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import liuds.music.App;
import liuds.music.R;
import liuds.music.model.Music;


public class Utils {
	private static Toast toast = null;
	private enum ToastType{
		SUCCESS,ERROR
	}
	public static void toast(final String title,ToastType type){
		View toastView;
		if(toast == null){
			toast = new Toast(App.getContext());
			toast.setDuration(Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);

			toastView = LayoutInflater.from(App.getContext()).inflate(R.layout.toast,null);

			toast.setView(toastView);
		}else{
			toastView = toast.getView();
		}

		ImageView icon = toastView.findViewById(R.id.icon);
		if(type != null){
			switch (type){
				case SUCCESS:
					icon.setImageResource(R.mipmap.success);
					break;
				case ERROR:
					icon.setImageResource(R.mipmap.warning);
					break;
			}
			icon.setVisibility(View.VISIBLE);
		}else{
			icon.setVisibility(View.GONE);
		}
		TextView titleView = toastView.findViewById(R.id.title);
		titleView.setText(title);
		toast.show();
	}

	public static void toast(String title){
		toast(title,null);
	}

	public static void toastSuccess(String title){
		toast(title,ToastType.SUCCESS);
	}

	public static void toastError(String title){
		toast(title,ToastType.ERROR);
	}

	public static int dip2px(float dip){
		final float scale = App.getContext().getResources().getDisplayMetrics().density;
		return (int)(dip * scale + 0.5f); 
	}

	public static int getVersionCode(Context context) {
		PackageManager pm = context.getPackageManager();
		try {
			PackageInfo packinfo = pm.getPackageInfo(context.getPackageName(),
					0);
			return packinfo.versionCode;
		} catch (NameNotFoundException e) {
			return 1;
		}
	}
	public static String getVersionName(Context context) {
		PackageManager pm = context.getPackageManager();
		try {
			PackageInfo packinfo = pm.getPackageInfo(context.getPackageName(),
					0);
			return packinfo.versionName;
		} catch (NameNotFoundException e) {
			return "unknown";
		}
	}

	public static String filter(String str){
		if(TextUtils.isEmpty(str)){
			return "";
		}
		return str.trim().replace("\n", "");
	}


	public static boolean inRangeOfView(View view, MotionEvent ev){
		int[] location = new int[2];
		view.getLocationOnScreen(location);
		int x = location[0];
		int y = location[1];
		if(ev.getX() < x || ev.getX() > (x + view.getWidth()) || ev.getY() < y || ev.getY() > (y + view.getHeight())){
			return false;
		}
		return true;
	}

	public static List<Music> getRandomList(List<Music> list){
		//数组长度
		int count = list.size();
		//结果集
		List<Music> resultList = new ArrayList<>();
		//用一个LinkedList作为中介
		LinkedList<Music> temp = new LinkedList<Music>();
		//初始化temp
		for(int i = 0; i<count; i++){
			temp.add(list.get(i));
		}
		//取数
		Random rand = new Random();
		for(int i = 0;i<count; i++){
			int num = rand.nextInt(count - i);
			resultList.add(temp.get(num));
			temp.remove(num);
		}

		return resultList;
	}

//	private static int[] calculatePopWindowPos(final View anchorView, final View contentView) {
//		final int windowPos[] = new int[2];
//		final int anchorLoc[] = new int[2];
//　　　　 // 获取锚点View在屏幕上的左上角坐标位置
//		anchorView.getLocationOnScreen(anchorLoc);
//		final int anchorHeight = anchorView.getHeight();
//		// 获取屏幕的高宽
//		final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());
//		final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
//		contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
//		// 计算contentView的高宽
//		final int windowHeight = contentView.getMeasuredHeight();
//		final int windowWidth = contentView.getMeasuredWidth();
//		// 判断需要向上弹出还是向下弹出显示
//		final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
//		if (isNeedShowUp) {
//			windowPos[0] = screenWidth - windowWidth;
//			windowPos[1] = anchorLoc[1] - windowHeight;
//		} else {
//			windowPos[0] = screenWidth - windowWidth;
//			windowPos[1] = anchorLoc[1] + anchorHeight;
//		}
//		return windowPos;
//	}

}
